/*
 * HelpWindow.java
 *
 * Created on March 16, 2007, 11:34 AM
 */

package busticketvendingsystem;
import java.io.IOException;
import javax.swing.*;
import javax.swing.tree.*;
import java.awt.*;

/**
 *
 * @author  Samik Saha
 */
public class HelpWindow extends javax.swing.JDialog {
    
    /** Creates new form HelpWindow */
    public HelpWindow(Frame parent,boolean modal,String topic) {
        super(parent,modal);
        initComponents();
        try {
            htmlPane.setPage(new HelpTopic("",topic).getTopicURL());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public HelpWindow(Dialog parent,boolean modal, String topic) {
        super(parent,modal);
        initComponents();
        try {
            htmlPane.setPage(new HelpTopic("",topic).getTopicURL());
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    public HelpWindow(Frame parent,boolean modal) {
        super(parent,modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jSplitPane1 = new javax.swing.JSplitPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        htmlPane = new javax.swing.JEditorPane();
        jScrollPane1 = new javax.swing.JScrollPane();
        DefaultMutableTreeNode top = new DefaultMutableTreeNode("BusTicketVendingSystem Help");
        createNodes(top);
        tree = new JTree(top);

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Bus Ticket Vending System Help");
        setIconImage(new javax.swing.ImageIcon(getClass().getResource("/busticketvendingsystem/resources/images/help.gif")).getImage());
        htmlPane.setEditable(false);
        try {
            htmlPane.setPage("file:///"+System.getProperty("user.dir")+"/help/default.htm");
        } catch (java.io.IOException e1) {
            e1.printStackTrace();
        }
        htmlPane.setPreferredSize(new java.awt.Dimension(400, 20));
        jScrollPane2.setViewportView(htmlPane);

        jSplitPane1.setRightComponent(jScrollPane2);

        jScrollPane1.setPreferredSize(new java.awt.Dimension(200, 324));
        tree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                treeValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(tree);

        jSplitPane1.setLeftComponent(jScrollPane1);

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 658, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jSplitPane1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 499, Short.MAX_VALUE)
                .addContainerGap())
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void treeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_treeValueChanged
        DefaultMutableTreeNode node = (DefaultMutableTreeNode)
                           tree.getLastSelectedPathComponent();

        if (node == null) return;

        Object nodeInfo = node.getUserObject();
        if (node.isLeaf()) {
            HelpTopic topic = (HelpTopic)nodeInfo;
            try {
                htmlPane.setPage(new java.net.URL(topic.getTopicURL()));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }//GEN-LAST:event_treeValueChanged
    private class HelpTopic {
        public String topicName;
        public String topicURL;

        public HelpTopic(String topic, String filename) {
            topicName = topic;
            topicURL = "file:///"+System.getProperty("user.dir")+"/help/"+filename;
            if (topicURL == null) {
                System.err.println("Couldn't find file: "
                                   + filename);
            }
        }

        public String toString() {
            return topicName;
        }
        public String getTopicURL(){
            return topicURL;
        }
    }

    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode category = null;
        DefaultMutableTreeNode subCategory = null;
        DefaultMutableTreeNode topic = null;
    
        category = new DefaultMutableTreeNode("Ticket Transaction");
        top.add(category);
    
        topic = new DefaultMutableTreeNode(new HelpTopic
        ("The main passenger form","passenger_form.htm"));
        category.add(topic);
    
        topic = new DefaultMutableTreeNode(new HelpTopic
        ("Confirming given info", "confirmation.htm"));
        category.add(topic);
    
        topic = new DefaultMutableTreeNode(new HelpTopic
        ("Paying money","payment.htm"));
        category.add(topic);

        category = new DefaultMutableTreeNode("Administrative use");
        top.add(category);

        topic = new DefaultMutableTreeNode(new HelpTopic
        ("Logging in","administrative_login.htm"));
        category.add(topic);
        
        subCategory = new DefaultMutableTreeNode("Using administrative tools");
        category.add(subCategory);
        
        topic = new DefaultMutableTreeNode(new HelpTopic("The administrative tools","administrative_tools.htm"));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Viewing transaction details by bus","transaction_details_by_bus_no.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Viewing transaction details by date","transaction_details_by_date.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Inserting a stoppage","ins_stoppage.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Updating a stoppage","update_stoppage.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Removing a stoppage","del_stoppage.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Inserting a new bus","ins_bus.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Updating a bus","update_bus.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Removing a bus","del_bus.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("Changing fare","change_fare.htm" ));
        subCategory.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("System requirements","system_requirements.htm" ));
        top.add(topic);
        
        topic = new DefaultMutableTreeNode(new HelpTopic
                ("About this software","about.htm" ));
        top.add(topic);
}


    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new HelpWindow(null,true).setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JEditorPane htmlPane;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JTree tree;
    // End of variables declaration//GEN-END:variables
    
    
}
